package com.risk.common.utils;


import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class HttpRequestUtil {

    /**
     * post模拟form表单
     *
     * @param url 请求地址
     * @param param 参数列表
     * @return 响应结果字符串
     */
    public static String postForm(String url, Map<String, String> param, String appCode) {
        // 创建默认的httpClient实例.
        CloseableHttpClient httpclient = HttpClients.createDefault();
        String r = null;
        // 创建httppost
        HttpPost httppost = new HttpPost(url);
        // 创建参数队列
        List<BasicNameValuePair> formparams = new ArrayList<BasicNameValuePair>();
        for (String key : param.keySet()) {
            formparams.add(new BasicNameValuePair(key, param.get(key)));
        }
        httppost.setHeader("Authorization", "APPCODE " + appCode);
        UrlEncodedFormEntity uefEntity;
        try {
            uefEntity = new UrlEncodedFormEntity(formparams, "UTF-8");
            httppost.setEntity(uefEntity);
            System.out.println("executing request " + httppost.getURI());
            CloseableHttpResponse response = httpclient.execute(httppost);
            r = request(response);
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e1) {
            e1.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 关闭连接,释放资源
            try {
                httpclient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return r;
    }

    /**
     * postJson
     *
     * @param url 请求地址
     * @param param 参数列表
     * @return 响应结果字符串
     */
    public static String post(String url, Map<String, String> param) {
        // 创建默认的httpClient实例.
        CloseableHttpClient httpclient = HttpClients.createDefault();
        String r = null;
        // 创建httppost
        HttpPost httppost = new HttpPost(url);
        // 创建参数队列
        JSONObject jsonParam = new JSONObject();
        for (String key : param.keySet()) {
            jsonParam.put(key, param.get(key));
        }
        StringEntity uefEntity;
        try {
            uefEntity = new StringEntity(jsonParam.toString(), "UTF-8");
            uefEntity.setContentEncoding("UTF-8");
            uefEntity.setContentType("application/json");
            httppost.setEntity(uefEntity);

            System.out.println("executing request " + httppost.getURI());
            CloseableHttpResponse response = httpclient.execute(httppost);
            r = request(response);
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e1) {
            e1.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 关闭连接,释放资源
            try {
                httpclient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return r;
    }

    /**
     * post xml
     *
     * @param url 请求地址
     * @return 响应结果字符串
     */
    public static String post(String url, String xml) {
        // 创建默认的httpClient实例.
        CloseableHttpClient httpclient = HttpClients.createDefault();
        String r = null;
        // 创建httppost
        HttpPost httppost = new HttpPost(url);
        StringEntity uefEntity;
        try {
            uefEntity = new StringEntity(xml, "UTF-8");
            uefEntity.setContentEncoding("UTF-8");
            uefEntity.setContentType("application/xml");
            httppost.setEntity(uefEntity);

            System.out.println("executing request " + httppost.getURI());
            CloseableHttpResponse response = httpclient.execute(httppost);
            r = request(response);
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e1) {
            e1.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 关闭连接,释放资源
            try {
                httpclient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return r;
    }


    public static String request(CloseableHttpResponse response) throws IOException {
        String r = null;
        try {
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                r = EntityUtils.toString(entity, "UTF-8");
                System.out.println("--------------------------------------");
                System.out.println("Response content: " + r);
                System.out.println("--------------------------------------");

            }
        }  finally {
            response.close();
        }
        return r;
    }
}
